#ifndef __YY_EVENT_BUS_LITE_H__
#define __YY_EVENT_BUS_LITE_H__

/*
 * Lightweight event bus
 *
 * history:
 *  2021-06-13 yao yu created
 *                      add class functor handler
 */

#include <map>
#include <list>

typedef void (*EventCB)(int msgid, void **pUser);

struct Observer
{
    virtual void operator()(int msgid, void **pUser) = 0;
};

struct EventBusLite
{
    bool attach(int msgid, EventCB callback);
    bool attach(int msgid, Observer *functor);

    bool detach(int msgid, EventCB callback);
    bool detach(int msgid, Observer *functor);

    void notify(int msgid, void **pUser);

private:
    std::map<int, std::list<EventCB>> m_events_map;
    std::map<int, std::list<Observer *>> m_functor_map;
};

/*
 * global event bus
 */

bool ge_attach(int msgid, EventCB callback);
bool ge_detach(int msgid, EventCB callback);

bool ge_attach(int msgid, Observer *functor);
bool ge_detach(int msgid, Observer *functor);

void ge_notify(int msgid, void **pUser);

#endif //#ifndef __YY_EVENT_BUS_LITE_H__
